#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <errno.h>
#include <android/log.h>

#define TAG "Socket_Client"
#define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG, TAG, __VA_ARGS__);
#define ERROR(...) __android_log_print(ANDROID_LOG_ERROR, TAG, __VA_ARGS__);

#define UNIX_DOMAIN "/tmp/UNIX_SOCKET"

int main(int argc, char **argv) {
    struct sockaddr_un srv_addr;
    int fd, ret;
    char snd_buf[1024];

    // 1.创建client socket
    fd = socket(PF_UNIX, SOCK_STREAM, 0);
    if (fd < 0) {
        ERROR("Failed to open socket '%s': %s\n", UNIX_DOMAIN, strerror(errno));
        return -1;
    }

    // 2.connect
    srv_addr.sun_family=AF_UNIX;
    strcpy(srv_addr.sun_path, UNIX_DOMAIN);
    ret=connect(fd, (struct sockaddr*)&srv_addr, sizeof(srv_addr));
    if(ret==-1)
    {
        ERROR("Failed to connect to the server");
        goto out_close;
    }

    // 3.write read
    memset(snd_buf, 0, 1024);
    strcpy(snd_buf,"Client say hello!");
    write(fd, snd_buf, sizeof(snd_buf));

    // 4.close
    close(fd);
    return 0;

out_close:
    close(fd);
    return -1;
}